﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shop.Managers;
using Data.Repositories;
using Data.Db;
using Data.Mappers;
using Shop.Models;
using Shop.Mappers;
using Shop.Areas.Admin.Models;

namespace Shop.Areas.Admin.Controllers
{
    public class ProductsController : Controller
    {
        private IWebMapper mapper;
        private IManagerProduct managerProducts;
        private IManager<Entity.Categories.Category> managerCategories;


        public ProductsController(IManagerProduct managerProducts, IManager<Entity.Categories.Category> managerCategories, IWebMapper mapper)
        {
            this.managerProducts = managerProducts;
            this.managerCategories = managerCategories;
            this.mapper = mapper;
        }

        public ActionResult Index()
        {
            return View(mapper.Wrapper(managerProducts.All.ToList()));
        }

        [HttpGet]
        public ActionResult Create()
        {
            var categories = managerCategories.All.ToList();
            Shop.Models.Product product = new Shop.Models.Product() { Categories = categories };
            return View(product);
        }

        [HttpPost]
        public ActionResult Create(Shop.Models.Product product, string[] idCategories)
        {
            product.Categories = new List<Entity.Categories.Category>();

            if(idCategories != null)
                foreach (string i in idCategories)
                    if (!i.Contains("true") && !i.Contains("false"))
                        product.Categories.Add(managerCategories.Find(Int32.Parse(i)));

            managerProducts.Add(mapper.Wrapper(product));
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Delete(int IdProduct)
        {
            managerProducts.Remove(IdProduct);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Update(int IdProduct)
        {
            ViewBag.Categories = managerCategories.All.ToList();
            Entity.Products.Product prod = managerProducts.Find(IdProduct);
            return View(mapper.Wrapper(prod));
        }

        [HttpPost]
        public ActionResult Update(Shop.Models.Product product)
        {
            managerProducts.Update(mapper.Wrapper(product));
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult UpdateCategories(int IdProduct)
        {
            ModelUpdateCategories modelUpdateCategories = new ModelUpdateCategories();
            modelUpdateCategories.Categories = new List<Entity.Categories.Category>();
            modelUpdateCategories.IdProduct = IdProduct;

            Entity.Products.Product prod = managerProducts.Find(IdProduct);

            ViewBag.Categories = managerCategories.All;

            foreach (var cat in prod.Categories)
                modelUpdateCategories.Categories.Add(cat);

            return View(modelUpdateCategories);
        }

        [HttpPost]
        public ActionResult UpdateCategories(int IdProduct, string[] idCategories)
        {
            Entity.Products.Product product = managerProducts.Find(IdProduct);
            product.Categories.Clear();

            if (idCategories != null)
                foreach (string i in idCategories)
                    if (!i.Contains("true") && !i.Contains("false"))
                        product.Categories.Add(managerCategories.Find(Int32.Parse(i)));

            managerProducts.UpdateCategories(product);
            return RedirectToAction("Index");
        }


    }
}
